using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Security;
using System.Web.UI;
using System.Data;
using System.Xml;
using System.Web.Services.Protocols;

namespace QMWebParts
{
    
    public class QMV5Delivery : WebPart
    {
        string _qmwiseURL = "http://localhost/qmwise5/qmwise.asmx";
        string _linkText = "Show my assessments";
        string _clientID = "Manager";
        string _checkSum = "434de524caad1f0bd4983c4cbf0cd0e8";
        QMWISeProxy.QMWISe service = new QMWebParts.QMWISeProxy.QMWISe();

        protected override void Render(HtmlTextWriter output)
        {   
            try
            {
                service.Url = _qmwiseURL;
                service.Security = new QMWebParts.QMWISeProxy.SecurityHeader();
                service.Security.ClientID = _clientID;
                service.Security.Checksum = _checkSum;
                
                if (this.Parent.GetType().FullName == "Microsoft.SharePoint.WebPartPages.WebPartPreview")
                    output.Write(BuildPReview());
                else
                    output.Write(BuildAssessmentLinks());
            }
            catch (Exception ex)
            {
                string st = "<BR>Message : " + ex.Message + "<BR>Stack Trace : " + ex.StackTrace + "<BR>Stack Trace : " + ex.ToString();
                output.Write("<BR>" + st);
            }
        }

        private string BuildAssessmentLinks()
        {
            string _user = GetUserName();
            QMWISeProxy.Schedule[] scArray = null;
            StringBuilder strURL = new StringBuilder();
            string url = string.Empty;
            string participantID = string.Empty;
            try
            {
                participantID = service.GetParticipantByName(_user).Participant_ID;

                service.Security.ClientID = _clientID;
                service.Security.Checksum = _checkSum;
                scArray = service.GetScheduleListByParticipant(participantID);
                strURL.Append("<ul>");
                foreach (QMWISeProxy.Schedule schedule in scArray)
                {
                    service.Security.ClientID = _clientID;
                    service.Security.Checksum = _checkSum;
                    url = service.GetAccessAssessment(schedule.Assessment_ID, _user, "", "");

                    if (!string.IsNullOrEmpty(url))
                        strURL.Append("<li><a href='" + url + "' target=_blank><B>" + schedule.Schedule_Name + "</B></a>");

                    url = "";
                }
                strURL.Append("<ul>");
            }
            catch (SoapException sopex)
            {
                return sopex.Message;                
                
            }
            catch (Exception ex)
            {

                return ex.Message;
            }
                        
            return strURL.ToString();

        }

        private string BuildPReview()
        {

            StringBuilder previewString = new StringBuilder();
            QMWISeProxy.Version version = null;
            try
            {
                service.Security = new QMWebParts.QMWISeProxy.SecurityHeader();
                service.Security.Checksum = _checkSum;
                service.Security.ClientID = _clientID;
                version = service.GetAbout();
                previewString.Append("<Table border=0 cellpadding=2>");
                previewString.Append("<TR><TD colspan=2 align=center>");
                previewString.Append("<B> Sucessfully connected to QMWISe.");
                previewString.Append("</TD></TR>");
                previewString.Append("<TR><TD>");
                previewString.Append(" QMWISe URL ");
                previewString.Append("</TD><TD>");
                previewString.Append(" : "+_qmwiseURL);
                previewString.Append("</TD></TR>");
                previewString.Append("<TR><TD>");
                previewString.Append(" Perception version ");
                previewString.Append("</TD><TD>");
                previewString.Append(" : "+version.BuildString.ToString());
                previewString.Append("</TD></TR>");                                
                previewString.Append("</TABLE>");
                
            }
            catch(Exception ex)
            {
                previewString.Append("Could not connect to QMWISe, please check your connection details.");
                previewString.Append("<BR>'" + service.Url.ToString() + "'<BR>" + ex.Message);
            }
            return previewString.ToString();
        }

        private string GetUserName()
        {
            string _user = string.Empty;
            string[] username = System.Web.HttpContext.Current.User.Identity.Name.Split(new char[] { '\\' });

            if (username.Length > 0)
                _user = username[username.Length - 1].ToString();
            else
                _user = System.Web.HttpContext.Current.User.Identity.Name;

            return _user;
        }

        [WebBrowsable(true), WebDescription("QMWISe URL"), WebDisplayName("QMWISe URL"), Personalizable(PersonalizationScope.Shared)]
        public string QMWISeURL
        {
            get { return _qmwiseURL; }
            set { _qmwiseURL = value; }

        }

        [WebBrowsable(true), WebDescription("Client ID"), WebDisplayName("Client ID"), Personalizable(PersonalizationScope.User)]
        public string Client_ID
        {
            get { return _clientID; }
            set { _clientID = value; }

        }

        [WebBrowsable(true), WebDescription("Check Sum"), WebDisplayName("Check Sum"), Personalizable(PersonalizationScope.User)]
        public string CheckSum
        {
            get { return _checkSum; }
            set { _checkSum = value; }

        }


        [WebBrowsable(true), WebDescription("Link Text"), WebDisplayName("Link Text"), Personalizable(PersonalizationScope.Shared)]
        public string LinkText
        {
            get { return _linkText; }
            set { _linkText = value; }

        }

        
    }
}
